package com.zy.algorithm.v3.array

import android.content.Intent
import com.zy.view.DIYSortView
import com.zy.utils.data.Point
import com.zy.utils.data.auto.AutoIndexElement
import com.zy.utils.data.auto.AutoNodeStep
import com.zy.utils.data.auto.Coordinate
import java.util.ArrayList
import java.util.Random
import kotlin.math.max

//https://leetcode.cn/problems/add-two-numbers/
//2. 两数相加
open class LeetCode0002 : BaseV3ArrayFragment() {
    //链表
    private var array1 = intArrayOf(2, 4, 3, 7)
    private var array2 = intArrayOf(1, 2, 3, 1)

    private val baseIX = 3
    private val l1Y = 2
    private val l2y = 3
    private val l3y = 5

    //进位标志
    private val carryNode =
        builder.setOriginX(baseIX).setOriginY(1).setV("0").setIndex(999).setCircle(true)
            .build(Point.Type.DEFAULT)

    override fun getInitD(): Int {
        return 150
    }

    override fun setData(diyV4: DIYSortView) {
        title.text = "两数相加"
        //diyV4.changeType = 1
        diyV4.setD(getInitD())
        if (code == 1) { //有进位
            array1 = intArrayOf(9, 9, 9, 9, 9)
            array2 = intArrayOf(5, 6, 7)
        } else { //随机
            //5
            for (i in array1.indices) {
                array1[i] = Random().nextInt(9)
            }
            //3
            for (i in array2.indices) {
                array2[i] = Random().nextInt(9)
            }
        }

//        diyV4.create(4, 2, array1, Point.Type.ROUND_INDEX)
//        diyV4.create(4, 3, array2, Point.Type.ROUND_INDEX)


        array1.forEachIndexed { index, i ->
            diyV4.addPoint(
                builder.setOriginX(baseIX + index * 2).setOriginY(l1Y).setV(i.toString())
                    .setIndex(0).setCircle(true)
                    .build(Point.Type.DEFAULT)
            )
            if (index != array1.lastIndex) {
                diyV4.addPoint(
                    builder.setOriginX(baseIX + index * 2 + 1).setOriginY(l1Y).setIndex(0)
                        .build(Point.Type.LINE)
                )
            }
        }

        array2.forEachIndexed { index, i ->
            diyV4.addPoint(
                builder.setOriginX(baseIX + index * 2).setOriginY(l2y).setV(i.toString())
                    .setIndex(0).setCircle(true)
                    .build(Point.Type.DEFAULT)
            )
            if (index != array2.lastIndex) {
                diyV4.addPoint(
                    builder.setOriginX(baseIX + index * 2 + 1).setOriginY(l2y).setIndex(0)
                        .build(Point.Type.LINE)
                )
            }
        }


        addAss()
    }

    private fun getVArray(intarray: IntArray, index: Int): Int {
        if (index < intarray.size) {
            return intarray[index]
        }
        return 0
    }

    private var i = -1

    private var carryV = 0

    private fun indexAddAdd() {
        i++
        val maxSize = max(array1.size, array2.size)
        if (i > maxSize) {
            return
        }

        if (i == maxSize && carryV == 0) {
            return
        }

        val sum = getVArray(array1, i) + getVArray(array2, i) + carryV

        carryV = sum / 10
        carryNode.v = carryV.toString()

        if (i != 0) {
            diyV4.addPoint(
                builder.setOriginX(baseIX + i * 2 - 1).setOriginY(l3y).setIndex(0)
                    .build(Point.Type.LINE)
            )
        }

        diyV4.addPoint(
            builder.setOriginX(baseIX + i * 2).setOriginY(l3y).setV((sum % 10).toString())
                .setCircle(true)
                .build(Point.Type.DEFAULT)
        )


        val list: MutableList<AutoNodeStep> = ArrayList<AutoNodeStep>()
        //数值


        if (i < maxSize) {
            val stepList = listOf(
                AutoIndexElement(999, Coordinate(baseIX + i * 2 + 2, 1)),
            )
            list.add(AutoNodeStep(stepList))

            diyV4.autoMove(list)
        } else {
            diyV4.invalidate()
        }
    }

    //adb shell am broadcast -a com.zy.test.DataTest --ei code 1
    override fun onDIYReceive(intent: Intent) {
        indexAddAdd()
    }

    override fun autoClick() {
        indexAddAdd()
    }

    private fun addAss() {
        diyV4.addPoint(carryNode)
    }
}